package com.gyg.service.impl;

import com.gyg.constant.CloudConstant;
import com.gyg.domain.CloudVisit;
import com.gyg.dao.CloudVisitMapper;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.service.CloudVisitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.vo.result.VisitResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 云盘访问量表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-04-14
 */
@Service
public class CloudVisitServiceImpl extends ServiceImpl<CloudVisitMapper, CloudVisit> implements CloudVisitService {

    @Override
    public RespEntity<VisitResult> visitList() {
        //最近24小时访问量数据
        List<CloudVisit> hourVisitList = baseMapper.listRecent24HVisit();

        //最近30日访问量数据
        List<CloudVisit> dayVisitList = baseMapper.listRecent30DaysVisit();

        //数据组装
        VisitResult visitResult = dataPackage(hourVisitList, dayVisitList);

        return new RespEntity(RespCode.SUCCESS, visitResult);
    }

    /**
     * 数据组装
     * @param hourVisitList
     * @param dayVisitList
     * @return
     */
    private VisitResult dataPackage(List<CloudVisit> hourVisitList, List<CloudVisit> dayVisitList) {
        //近24小时日期集合
        List<String> hourDateList = hourVisitList.stream().map(CloudVisit::getDt).collect(Collectors.toList());
        hourDateList = hourDateList.stream().map(v -> {
            String[] split = v.split("\\s+")[1].split(":");
            return split[0] + ":" + split[1];
        }).collect(Collectors.toList());

        //近24小时访问量集合
        List<Integer> hourValueList = hourVisitList.stream().map(CloudVisit::getVisit).collect(Collectors.toList());
        //近24小时访问量最大值
        Integer hourMaxValue = hourValueList.stream().max(Integer::compareTo).get();

        //近30天日期集合
        List<String> dayDateList = dayVisitList.stream().map(CloudVisit::getDtD).collect(Collectors.toList());
        dayDateList = dayDateList.stream().map(v -> {
            String[] split = v.split("-");
            return split[1] + "-" + split[2];
        }).collect(Collectors.toList());

        //近30天访问量集合
        List<Integer> dayValueList = dayVisitList.stream().map(CloudVisit::getVisit).collect(Collectors.toList());
        //近30天访问量最大值
        Integer dayValueMax = dayValueList.stream().max(Integer::compareTo).get();

        return new VisitResult(hourMaxValue, hourDateList, hourValueList, dayValueMax, dayDateList, dayValueList);
    }
}
